<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二维码生成工具 - 在线工具集合</title>
    <script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
            position: relative;
            overflow-x: hidden;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle at 50% 50%, rgba(255,255,255,0.1) 0%, transparent 50%);
            pointer-events: none;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
            position: relative;
            z-index: 1;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .input-section {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
            backdrop-filter: blur(10px);
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group label {
            display: block;
            margin-bottom: 10px;
            color: #00ff88;
            font-weight: 500;
        }

        .input-group input, .input-group select, .input-group textarea {
            width: 100%;
            padding: 12px;
            border: none;
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .input-group input:focus, .input-group select:focus, .input-group textarea:focus {
            outline: none;
            border-color: #00ff88;
            box-shadow: 0 0 10px rgba(0, 255, 136, 0.2);
        }

        .input-group textarea {
            height: 100px;
            resize: vertical;
        }

        .options-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }

        .option-group {
            background: rgba(255, 255, 255, 0.05);
            padding: 15px;
            border-radius: 12px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        .option-group label {
            display: block;
            margin-bottom: 10px;
            color: rgba(255, 255, 255, 0.8);
            font-size: 0.9em;
        }

        .color-picker {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .color-picker input[type="color"] {
            width: 40px;
            height: 40px;
            padding: 0;
            border: none;
            border-radius: 6px;
            cursor: pointer;
        }

        .preview-section {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
            backdrop-filter: blur(10px);
            text-align: center;
        }

        .qr-container {
            display: inline-block;
            padding: 40px;
            background: #fff;
            border-radius: 16px;
            margin-bottom: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
            position: relative;
            overflow: hidden;
        }

        .qr-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(45deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
            pointer-events: none;
        }

        #qrcode {
            display: block;
            position: relative;
            z-index: 1;
        }

        #qrcode img {
            display: block;
            border-radius: 8px;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 20px;
        }

        .action-btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 255, 136, 0.3);
        }

        .action-btn:disabled {
            background: #666;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        .hint-text {
            margin-top: 5px;
            font-size: 12px;
            color: rgba(255, 255, 255, 0.6);
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px 10px;
            }

            .header h1 {
                font-size: 2em;
            }

            .options-grid {
                grid-template-columns: 1fr;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>二维码生成工具</h1>
            <p>生成自定义二维码，支持多种样式和颜色</p>
        </div>

        <div class="input-section">
            <div class="input-group">
                <label>二维码内容类型</label>
                <div class="custom-select">
                    <select id="contentType">
                        <option value="text">文本</option>
                        <option value="url">网址</option>
                        <option value="wifi">WiFi</option>
                        <option value="vcard">联系人</option>
                        <option value="tel">电话号码</option>
                        <option value="sms">短信</option>
                        <option value="email">邮箱</option>
                    </select>
                </div>
            </div>

            <div class="input-group">
                <label>二维码内容</label>
                <textarea id="content" placeholder="输入要生成二维码的内容..."></textarea>
                <div id="contentHint" class="hint-text"></div>
            </div>

            <div class="options-grid">
                <div class="option-group">
                    <label>二维码大小</label>
                    <input type="number" id="size" value="200" min="100" max="1000" step="10">
                </div>
                <div class="option-group">
                    <label>纠错级别</label>
                    <div class="custom-select">
                        <select id="errorLevel">
                            <option value="L">低 (7%)</option>
                            <option value="M">中 (15%)</option>
                            <option value="Q">较高 (25%)</option>
                            <option value="H">高 (30%)</option>
                        </select>
                    </div>
                </div>
                <div class="option-group">
                    <label>前景颜色</label>
                    <div class="color-picker">
                        <input type="color" id="foreground" value="#000000">
                        <input type="text" id="foregroundHex" value="#000000" readonly>
                    </div>
                </div>
                <div class="option-group">
                    <label>背景颜色</label>
                    <div class="color-picker">
                        <input type="color" id="background" value="#FFFFFF">
                        <input type="text" id="backgroundHex" value="#FFFFFF" readonly>
                    </div>
                </div>
            </div>
        </div>

        <div class="preview-section">
            <div class="qr-container">
                <div id="qrcode"></div>
            </div>

            <div class="action-buttons">
                <button class="action-btn" onclick="generateQR()">生成二维码</button>
                <button class="action-btn" onclick="downloadQR()">下载二维码</button>
            </div>
        </div>
    </div>

    <script>
        // 获取DOM元素
        const contentType = document.getElementById('contentType');
        const content = document.getElementById('content');
        const size = document.getElementById('size');
        const errorLevel = document.getElementById('errorLevel');
        const foreground = document.getElementById('foreground');
        const background = document.getElementById('background');
        const foregroundHex = document.getElementById('foregroundHex');
        const backgroundHex = document.getElementById('backgroundHex');
        const qrcode = document.getElementById('qrcode');
        const contentHint = document.getElementById('contentHint');

        // 更新颜色显示
        foreground.addEventListener('input', (e) => {
            foregroundHex.value = e.target.value;
            if (content.value.trim()) {
                generateQR();
            }
        });

        background.addEventListener('input', (e) => {
            backgroundHex.value = e.target.value;
            if (content.value.trim()) {
                generateQR();
            }
        });

        // 更新提示文本
        function updateHint() {
            const type = contentType.value;
            let hint = '';
            switch(type) {
                case 'url':
                    hint = '请输入完整的网址，例如：https://www.example.com';
                    break;
                case 'wifi':
                    hint = '格式：WIFI:S:<网络名称>;T:<加密类型>;P:<密码>;;';
                    break;
                case 'vcard':
                    hint = '格式：BEGIN:VCARD\nVERSION:3.0\nN:姓名\nTEL:电话\nEND:VCARD';
                    break;
                case 'tel':
                    hint = '请输入电话号码，例如：+8613800138000';
                    break;
                case 'sms':
                    hint = '格式：SMSTO:电话号码:短信内容';
                    break;
                case 'email':
                    hint = '请输入邮箱地址，例如：example@example.com';
                    break;
                default:
                    hint = '请输入要生成二维码的文本内容';
            }
            contentHint.textContent = hint;
        }

        // 生成二维码
        function generateQR() {
            let text = content.value.trim();
            if (!text) {
                toast.warning('请输入二维码内容');
                return;
            }

            try {
                // 根据内容类型格式化文本
                const type = contentType.value;
                switch(type) {
                    case 'url':
                        if (!text.startsWith('http://') && !text.startsWith('https://')) {
                            text = 'https://' + text;
                            toast.info('已自动添加 https:// 前缀');
                        }
                        break;
                    case 'wifi':
                        if (!text.startsWith('WIFI:')) {
                            text = 'WIFI:S:' + text + ';T:WPA;P:;';
                            toast.info('已自动添加 WiFi 格式前缀');
                        }
                        break;
                    case 'tel':
                        if (!text.startsWith('tel:')) {
                            text = 'tel:' + text;
                            toast.info('已自动添加 tel: 前缀');
                        }
                        break;
                    case 'sms':
                        if (!text.startsWith('SMSTO:')) {
                            text = 'SMSTO:' + text;
                            toast.info('已自动添加 SMSTO: 前缀');
                        }
                        break;
                    case 'email':
                        if (!text.startsWith('mailto:')) {
                            text = 'mailto:' + text;
                            toast.info('已自动添加 mailto: 前缀');
                        }
                        break;
                }

                // 清除旧的二维码
                qrcode.innerHTML = '';

                // 生成新的二维码
                new QRCode(qrcode, {
                    text: text,
                    width: parseInt(size.value),
                    height: parseInt(size.value),
                    colorDark: foreground.value,
                    colorLight: background.value,
                    correctLevel: QRCode.CorrectLevel[errorLevel.value]
                });
                toast.success('二维码生成成功');
            } catch (error) {
                console.error('生成二维码时出错:', error);
                toast.error('生成二维码失败，请检查输入内容');
            }
        }

        // 下载二维码
        function downloadQR() {
            const canvas = qrcode.querySelector('canvas');
            if (!canvas) {
                toast.warning('请先生成二维码');
                return;
            }

            try {
                // 创建一个新的画布，用于添加内边距
                const padding = 40;
                const newCanvas = document.createElement('canvas');
                const ctx = newCanvas.getContext('2d');
                
                // 设置新画布的尺寸（包含内边距）
                newCanvas.width = canvas.width + padding * 2;
                newCanvas.height = canvas.height + padding * 2;

                // 填充白色背景
                ctx.fillStyle = '#FFFFFF';
                ctx.fillRect(0, 0, newCanvas.width, newCanvas.height);

                // 绘制二维码
                ctx.drawImage(canvas, padding, padding);
                
                const link = document.createElement('a');
                link.download = 'qrcode.png';
                link.href = newCanvas.toDataURL('image/png');
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
                toast.success('二维码已下载');
            } catch (error) {
                console.error('下载二维码时出错:', error);
                toast.error('下载二维码失败，请重试');
            }
        }

        // 监听选项变化
        size.addEventListener('change', () => {
            if (content.value.trim()) {
                generateQR();
            }
        });

        errorLevel.addEventListener('change', () => {
            if (content.value.trim()) {
                generateQR();
            }
        });

        // 监听内容类型变化
        contentType.addEventListener('change', () => {
            updateHint();
            content.value = ''; // 清空内容
            generateQR();
        });

        // 初始化提示文本
        updateHint();

        // 自动生成
        let timeout;
        content.addEventListener('input', () => {
            clearTimeout(timeout);
            timeout = setTimeout(() => {
                if (content.value.trim()) {
                    generateQR();
                }
            }, 500);
        });

        // 设置示例内容
        content.value = 'https://example.com';
        generateQR();
    </script>
</body>
</html> 